/**   
 * @Title: BeidanRecordDetailAct.java 
 * @Package com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord 
 * @Description: 北单购彩记录详情
 * @author: City Jiangsy@500wan.com   
 * @date: 2012-1-11 下午1:57:26  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.ImageZhanJi;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.HemaiDetailInfo;
import com.esun.model.UserInfo;
import com.esun.model.beijingdanchang.BeidanHemaiDetailInfo;
import com.esun.model.beijingdanchang.BeidanHemaiDetailTeamInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.CheDanCommunicater;
import com.esun.rabbit2.communicaters.GetUserIconCommunicater;
import com.esun.rabbit2.communicaters.lotteryrecord.GetBeijingdanchangRecordDetailCommunicater;
import com.esun.rabbit2.viewer.mainact.OptionAct;
import com.esun.ui.R;
import com.esun.util.ShowZhanji;
import com.esun.util.ViewerUtil;
import com.esun.util.img.ImageUtils;
import com.esun.util.img.db.ImageDB;
import com.esun.util.log.EDebug;

/**
 * @ClassName: BeidanRecordDetailAct
 * @Description: 北单购彩记录详情
 * @author: City Jiangsy@500wan.com
 * @date: 2012-1-11 下午1:57:26
 * 
 */
public class BeidanRecordDetailAct extends Activity implements OnClickListener {
	private TextView tvTerm;
	private TextView tvTime;
	private TextView tvProId;
	// private TextView tvRestCount;

	private TextView tvFaqiren;
	private TextView tvHitCount;
	private TextView tvHitMoney;
	private TextView tvAllMoney;
	private TextView tvMultiple;
	private TextView tvAllCount;
	private TextView tvEachMoney;
	private TextView tvBaodiMoney;
	private TextView tvTicheng;
	private TextView tvIssuc;
	private TextView tvJindu;
	private TextView tvGGtype;
	private ImageView imageFaqiren;
	private TextView tvIsShow;
	private TextView tvPlayName;

	private LinearLayout linearZhanji;
	private LinearLayout linearContent;
	private LinearLayout linearLayOutZhanji;

	private TextView tvLotteryState;
	private TextView tvWinMoney;

	private ImageButton btnCancel;
	private Button btnCheDan;// 撤单
	private Button btnConect;
	private ProgressDialog progress;

	private boolean isTXT = false;
	private String programId;
	private String uid;
	private String strfaqiren;
	private String proid;
	private String playId;
	@SuppressWarnings("unused")
	private String termNum;
	private String getManey;
	private String playName;

	private static final String NO_DATA = "--";

	private GetBeijingdanchangRecordDetailCommunicater hemaiGetDetailCommunicater;
	private GetUserIconCommunicater getUserIconCommunicater;
	private CheDanCommunicater cheDanCommunicater;

	private Handler handlerHemaiDetail;

	private static final int GET_DETAIL_SUCCEED = 1001;
	private static final int GET_DETAIL_FAIL = 1002;
	private final int CHEDAN_SUCCEED = 1003; // 撤单通信成功
	private final int CHEDAN_FAIL = 1004;// 撤单通信失败
	private static final int REPLACE_IMAGE = 1006;
	private static final int GET_ICONNAME_SUCCEED = 1007;
	private static final int GET_ICONNAME_FAIL = 1008;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.beidanrecordi_detail_act);
		initData();
		initHandler();
		initUi();
	}

	private void initHandler() {

		handlerHemaiDetail = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case REPLACE_IMAGE:
					Bitmap bm = (Bitmap) msg.obj;
					imageFaqiren.setImageBitmap(bm);

					break;
				case GET_DETAIL_SUCCEED:
					hemaiGetDetailCommunicater.updateDetailInfo();
					progress.dismiss();
					updateTvContent();
					break;

				case GET_DETAIL_FAIL:
					progress.dismiss();
					ViewerUtil.showTipDialog(BeidanRecordDetailAct.this, hemaiGetDetailCommunicater.getErrorMSG(), true);
					break;

				case Config.CONNECTION_FAIL:
					progress.dismiss();
					// ViewerUtil.showTipDialog(BeidanRecordDetailAct.this, Language.STR_CONNECTIONFAIL, true);
					Toast.makeText(BeidanRecordDetailAct.this, Language.STR_CONNECTIONFAIL.toString(), 0).show();
					break;

				case GET_ICONNAME_SUCCEED:
					getAvatar();
					break;

				case GET_ICONNAME_FAIL:
					EDebug.o(BeidanRecordDetailAct.this, "get icon name fail");
					break;

				case CHEDAN_SUCCEED:
					EDebug.o(this, "撤单成功");
					UserInfo.getUserInfo().setUserBalance(cheDanCommunicater.getMoney());
					ViewerUtil.showTipDialog(BeidanRecordDetailAct.this, "撤单成功", false);
					break;

				case CHEDAN_FAIL:
					EDebug.o(this, "撤单失败");
					String serverMsg = (String) msg.obj;
					ViewerUtil.showTipDialog(BeidanRecordDetailAct.this, serverMsg, false);
					break;

				default:
					break;
				}
			}

		};

	}

	private void updateTvContent() {

		BeidanHemaiDetailInfo beidanhemaiinfo = BeidanHemaiDetailInfo.getBeidanHemaiDetailInfo();
		termNum = beidanhemaiinfo.getExpect();
		proid = beidanhemaiinfo.getProid();
		playId = beidanhemaiinfo.getPlayid();
		playName = beidanhemaiinfo.getPlayname();
		strfaqiren = beidanhemaiinfo.getUsername();
		getManey = beidanhemaiinfo.getGetmoney();
		tvPlayName.setText(playName);
		tvProId.setText(proid);
		tvFaqiren.setText(strfaqiren);
		tvHitCount.setText(beidanhemaiinfo.getLotterycount());
		tvHitCount.setTextColor(getResources().getColor(R.color.kjxx_blue));
		tvHitMoney.setText(Language.POPLOTTERYTIP_RMB + beidanhemaiinfo.getLotterymonery());
		tvHitMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvAllMoney.setText(Language.POPLOTTERYTIP_RMB + beidanhemaiinfo.getAllmoney());
		tvAllMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvMultiple.setText(beidanhemaiinfo.getBeishu());
		tvAllCount.setText(beidanhemaiinfo.getAnumber());
		tvEachMoney.setText(Language.POPLOTTERYTIP_RMB + beidanhemaiinfo.getOnemoney());
		tvEachMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		tvBaodiMoney.setText(Language.POPLOTTERYTIP_RMB + beidanhemaiinfo.getBaodimoney());
		tvBaodiMoney.setTextColor(getResources().getColor(R.color.kjxx_red));
		String ticheng = beidanhemaiinfo.getTicheng();
		tvTicheng.setText(ticheng);

		// 是否显示撤单按钮
		if (beidanhemaiinfo.getIshm().equals(Config.NUM1)) {
			int baodi = Integer.valueOf(beidanhemaiinfo.getBaodi());
			int jindu = Integer.valueOf(beidanhemaiinfo.getJindu());
			if (baodi + jindu < 50) {
				btnCheDan.setVisibility(View.VISIBLE);
			}
		}

		if (beidanhemaiinfo.getIssuc().equals(Config.NUM1)) {
			tvIssuc.setText(Language.POPLOTTERYTIP_ISSUCYET);
		} else {
			tvIssuc.setText(Language.POPLOTTERYTIP_NOTISSUC);
		}
		tvJindu.setText(beidanhemaiinfo.getJindu() + Config.PERCENT);
		tvGGtype.setText(beidanhemaiinfo.getGgtypename());
		tvTime.setText(beidanhemaiinfo.getAddtime());
		if (beidanhemaiinfo.getFileorcode() != null && beidanhemaiinfo.getFileorcode().indexOf("txt") > -1) {
			isTXT = true;
			if (beidanhemaiinfo.getIsUpload().equals(Config.NUM1)) {
				if (beidanhemaiinfo.getUsername().equals(UserInfo.getUserInfo().getUserName())) {
					// 显示查看方案
					btnConect.setVisibility(View.VISIBLE);
					btnConect.setText("查看上传方案");
				} else {
					if (beidanhemaiinfo.getIsshow().equals(Config.NUM1)) {
						// 截止后公开
						tvIsShow.setVisibility(View.VISIBLE);
						tvIsShow.setText(Language.POPLOTTERYTIP_JIEZHIHOUGONGKAI);
					} else {
						// 显示查看方案
						btnConect.setVisibility(View.VISIBLE);
						btnConect.setText("查看上传方案");
					}
				}
			} else {
				// 方案未上传
				tvIsShow.setVisibility(View.VISIBLE);
				tvIsShow.setText(Language.POPLOTTERYTIP_FANGANWEISHANGCHUAN);
			}
		} else {
			isTXT = false;
			if (beidanhemaiinfo.getUsername().equals(UserInfo.getUserInfo().getUserName())) {
				// 显示投注内容
				if (playId.equals("150")) {
					tvIsShow.setVisibility(View.VISIBLE);
					tvIsShow.setText(Config.URLBEIDANFORWEB + programId);
					EDebug.o(BeidanRecordDetailAct.this, Config.URLBEIDANFORWEB + programId);
				} else {
					setProjectContent();
				}
			} else {
				if (beidanhemaiinfo.getIsshow().equals(Config.NUM1)) {
					// 截止后公开
					tvIsShow.setVisibility(View.VISIBLE);
					tvIsShow.setText(Language.POPLOTTERYTIP_JIEZHIHOUGONGKAI);
				} else {
					// 显示投注内容
					if (playId.equals("150")) {
						tvIsShow.setVisibility(View.VISIBLE);
						tvIsShow.setText(Config.URLBEIDANFORWEB + programId);
						EDebug.o(BeidanRecordDetailAct.this, Config.URLBEIDANFORWEB + programId);
					} else {
						setProjectContent();
					}
				}
			}
		}

		if (beidanhemaiinfo.getLotterystate().equals("0")) {
			tvLotteryState.setText(Language.MSG_DENGDAIKAIJIANG);
		} else if (beidanhemaiinfo.getLotterystate().equals("1")) {
			tvLotteryState.setText("未算奖");
		} else if (beidanhemaiinfo.getLotterystate().equals("2")) {
			tvLotteryState.setText("未中奖");
		} else if (beidanhemaiinfo.getLotterystate().equals("3")) {
			tvLotteryState.setText("未派奖");
		} else if (beidanhemaiinfo.getLotterystate().equals("4")) {
			tvLotteryState.setText("已派奖");
			tvWinMoney.setVisibility(View.VISIBLE);
			tvWinMoney.setText("￥" + getManey);
		}

		// 金钻
		String golddiamond = beidanhemaiinfo.getGoldDiamond();
		// 银钻
		String silverdiamond = beidanhemaiinfo.getSilverDiamond();
		// 金星
		String goldstar = beidanhemaiinfo.getGoldStar();
		// 银星
		String silverstar = beidanhemaiinfo.getSilverStar();
		// 金杯
		String goldcup = beidanhemaiinfo.getGoldCup();
		// 战绩显示
		zhanJiShow(golddiamond, silverdiamond, goldstar, silverstar, goldcup);
	}

	// 设置方案内容
	private void setProjectContent() {

		ArrayList<BeidanHemaiDetailTeamInfo> list = new ArrayList<BeidanHemaiDetailTeamInfo>();
		list = BeidanHemaiDetailInfo.getBeidanHemaiDetailInfo().getTeamList();

		// 方案数目
		int count = list.size();

		// 方案内容
		if (count < 8) {
			for (int i = 0; i < count; i++) {
				BeidanHemaiDetailTeamInfo beidanHemaiDetailTeamInfo = list.get(i);

				LinearLayout linear = (LinearLayout) addEachItem(beidanHemaiDetailTeamInfo);

				linearContent.addView(linear);
			}
		} else {
			for (int i = 0; i < 7; i++) {
				BeidanHemaiDetailTeamInfo beidanHemaiDetailTeamInfo = list.get(i);

				LinearLayout linear = (LinearLayout) addEachItem(beidanHemaiDetailTeamInfo);

				linearContent.addView(linear);
			}
			final Button btnMore = new Button(this);
			btnMore.setText("...");
			btnMore.setBackgroundResource(R.drawable.lotteryrecord_detail_btn_more);
			final ArrayList<BeidanHemaiDetailTeamInfo> list_final = list;
			btnMore.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 展示所有方案内容
					showAllGameInfo(list_final);
					btnMore.setVisibility(View.GONE);
				}
			});
			linearContent.addView(btnMore);
		}
	}

	private void showAllGameInfo(ArrayList<BeidanHemaiDetailTeamInfo> list) {
		// 方案数目
		int index = BeidanHemaiDetailInfo.getBeidanHemaiDetailInfo().getTeamList().size();
		BeidanHemaiDetailTeamInfo beidanHemaiDetailTeamInfo = list.get(index);
		for (int i = 7; i < index; i++) {

			LinearLayout linear = (LinearLayout) addEachItem(beidanHemaiDetailTeamInfo);

			linearContent.addView(linear);
		}
	}

	// 添加单注方案内容
	private View addEachItem(BeidanHemaiDetailTeamInfo beidanHemaiDetailTeamInfo) {

		LinearLayout linear = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.football_game_info, null);
		TextView tvMatchdate = (TextView) linear.findViewById(R.id.football_game_info_tv_matchdate);
		TextView tvHomesxname = (TextView) linear.findViewById(R.id.football_game_info_tv_homesxname);
		TextView tvRangqiu = (TextView) linear.findViewById(R.id.football_game_info_tv_rangqiu);
		TextView tvAwaysxname = (TextView) linear.findViewById(R.id.football_game_info_tv_awaysxname);
		TextView tvDanma = (TextView) linear.findViewById(R.id.football_game_info_tv_danma);
		TextView tvTouzhuNeirong = (TextView) linear.findViewById(R.id.football_game_info_tv_touzhuneirong);

		tvMatchdate.setText(beidanHemaiDetailTeamInfo.getOrdernum());
		tvHomesxname.setText(beidanHemaiDetailTeamInfo.getHometeamname());
		tvRangqiu.setText(Config.LEFT_ROUND_BR + beidanHemaiDetailTeamInfo.getRangqiu() + Config.RIGHT_ROUND_BR);
		tvAwaysxname.setText(beidanHemaiDetailTeamInfo.getGuestteamname());
		if ((beidanHemaiDetailTeamInfo.getIsdm().equals("1"))) {
			tvDanma.setText("(" + Language.POPLOTTERYTIP_DAN + ")");
		}
		// 设置投注内容
		tvTouzhuNeirong.setText(getShotinOutput(beidanHemaiDetailTeamInfo.getShotin(), beidanHemaiDetailTeamInfo.getResultspf()));
		return linear;
	}

	// 设置投注内容颜色等
	private SpannableString getShotinOutput(String strItem, String result) {
		SpannableString sp = new SpannableString(strItem);
		int k = -1;

		// result.equals(null)会报空指针,注意格式
		if (result != null && !result.equals(Config.NULLMARK)) {
			if (strItem.indexOf(result) > -1) {
				k = strItem.indexOf(result);
				sp.setSpan(new ForegroundColorSpan(Color.RED), k, k, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				if (k > 0) {
					sp.setSpan(new ForegroundColorSpan(Color.BLACK), 0, k - 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				}
				if (k != strItem.length() - 1) {
					sp.setSpan(new ForegroundColorSpan(Color.BLACK), k + 1, strItem.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				}
			} else {
				sp.setSpan(new ForegroundColorSpan(Color.BLACK), 0, strItem.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			}

		} else {
			sp.setSpan(new ForegroundColorSpan(Color.BLACK), 0, strItem.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		}

		return sp;
	}

	private void threadGetUserIcon() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean isSucceed = getUserIconCommunicater.isGetIconNameSucced(strfaqiren);
					if (isSucceed) {
						Message msg = handlerHemaiDetail.obtainMessage(GET_ICONNAME_SUCCEED, null);
						handlerHemaiDetail.sendMessage(msg);
					} else {
						Message msg = handlerHemaiDetail.obtainMessage(GET_ICONNAME_FAIL, null);
						handlerHemaiDetail.sendMessage(msg);
					}
				} catch (Exception e) {

					Message msg = handlerHemaiDetail.obtainMessage(GET_ICONNAME_FAIL, null);
					handlerHemaiDetail.sendMessage(msg);
				}
			}
		}.start();
	}

	private void threadGetDetail() {
		new Thread() {
			@Override
			public void run() {
				EDebug.o(BeidanRecordDetailAct.this, "通讯开始");
				super.run();
				try {
					boolean isGetSucceed = hemaiGetDetailCommunicater.isGetDetailSucced(programId);
					EDebug.o(BeidanRecordDetailAct.this, "通讯结果 = " + isGetSucceed);
					if (isGetSucceed) {
						Message msg = handlerHemaiDetail.obtainMessage(GET_DETAIL_SUCCEED, null);
						handlerHemaiDetail.sendMessage(msg);
					} else {
						Message msg = handlerHemaiDetail.obtainMessage(GET_DETAIL_FAIL, null);
						handlerHemaiDetail.sendMessage(msg);
					}
				} catch (Exception e) {

					EDebug.o(BeidanRecordDetailAct.this, e.toString());
					Message msg = handlerHemaiDetail.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerHemaiDetail.sendMessage(msg);
				}
			}
		}.start();
	}

	private void initData() {
		Bundle bundle = this.getIntent().getExtras();
		programId = bundle.getString(Key.KEY_ID);
		uid = bundle.getString(Key.KEY_UID);
	}

	private void initUi() {

		new ImageZhanJi();

		tvTerm = (TextView) findViewById(R.id.beidanrecorddetailact_tv_term);
		tvTime = (TextView) findViewById(R.id.beidanrecorddetailact_tv_time);
		tvProId = (TextView) findViewById(R.id.beidanrecorddetailact_tv_programId);
		tvPlayName = (TextView) findViewById(R.id.beidanrecorddetailact_tv_playname);

		imageFaqiren = (ImageView) findViewById(R.id.beidanrecorddetailact_image_faqiren);
		tvFaqiren = (TextView) findViewById(R.id.beidanrecorddetailact_tv_faqiren);
		tvHitCount = (TextView) findViewById(R.id.beidanrecorddetailact_tv_hitCount);
		tvHitMoney = (TextView) findViewById(R.id.beidanrecorddetailact_tv_hitMoney);
		tvAllMoney = (TextView) findViewById(R.id.beidanrecorddetailact_tv_allMoney);
		tvMultiple = (TextView) findViewById(R.id.beidanrecorddetailact_tv_multiple);
		tvAllCount = (TextView) findViewById(R.id.beidanrecorddetailact_tv_allCount);
		tvEachMoney = (TextView) findViewById(R.id.beidanrecorddetailact_tv_eachMoney);
		tvBaodiMoney = (TextView) findViewById(R.id.beidanrecorddetailact_tv_baodiMoney);
		tvTicheng = (TextView) findViewById(R.id.beidanrecorddetailact_tv_ticheng);
		tvIssuc = (TextView) findViewById(R.id.beidanrecorddetailact_tv_issuc);
		tvJindu = (TextView) findViewById(R.id.beidanrecorddetailact_tv_jindu);
		tvGGtype = (TextView) findViewById(R.id.beidanrecorddetailact_tv_ggtype);

		tvIsShow = (TextView) findViewById(R.id.beidanrecorddetailact_tv_isShow);

		linearContent = (LinearLayout) findViewById(R.id.beidanrecorddetailact_linear_content);

		btnCancel = (ImageButton) findViewById(R.id.beidanrecorddetailact_btn_cancel);
		btnCancel.setOnClickListener(this);
		btnConect = (Button) findViewById(R.id.beidanrecorddetailact_btn_detail);
		btnConect.setOnClickListener(this);
		btnCheDan = (Button) findViewById(R.id.beidanrecord_detailact_btn_cheDan);
		btnCheDan.setOnClickListener(this);

		progress = ViewerUtil.showProgressDialog(BeidanRecordDetailAct.this, "加载中...");
		progress.show();

		linearZhanji = (LinearLayout) findViewById(R.id.beidanrecorddetailact_linear_zhanji);
		linearLayOutZhanji = (LinearLayout) findViewById(R.id.beidanrecorddetailact_ll_zhanji);

		tvLotteryState = (TextView) findViewById(R.id.beidanrecorddetailact_tv_lotteryState);
		tvWinMoney = (TextView) findViewById(R.id.beidanrecorddetailact_tv_getMoney);

		tvTerm.setText("方案详情");

		tvTime.setText(NO_DATA);
		tvHitCount.setText(NO_DATA);
		tvHitMoney.setText(NO_DATA);
		tvAllMoney.setText(NO_DATA);
		tvMultiple.setText(NO_DATA);
		tvAllCount.setText(NO_DATA);
		tvEachMoney.setText(NO_DATA);
		tvBaodiMoney.setText(NO_DATA);
		tvTicheng.setText(NO_DATA);
		tvIssuc.setText(NO_DATA);
		tvJindu.setText(NO_DATA);
		tvGGtype.setText(NO_DATA);
		hemaiGetDetailCommunicater = new GetBeijingdanchangRecordDetailCommunicater();
		getUserIconCommunicater = new GetUserIconCommunicater();
		threadGetDetail();
		threadGetUserIcon();

	}

	/**
	 * 2012-3-15 kael修改 战绩计算及显示
	 * 
	 * @param strgolddiamond
	 * @param strsilverdiamond
	 * @param strgoldstar
	 * @param strsilverstar
	 * @param strgoldcup
	 */
	private void zhanJiShow(String strgolddiamond, String strsilverdiamond, String strgoldstar, String strsilverstar, String strgoldcup) {
		ShowZhanji zhanji = new ShowZhanji(BeidanRecordDetailAct.this);
		// 获取显示战绩星级图片的list
		ArrayList<ImageView> zhanjilist = new ArrayList<ImageView>();
		zhanjilist = zhanji.showzhanji(strgolddiamond, strsilverdiamond, strgoldstar, strsilverstar, strgoldcup);
		// 判断是否有战绩
		int size = zhanjilist.size();
		if (size == 0) {// 没有战绩就不显示战绩的linear
			linearLayOutZhanji.setVisibility(View.GONE);
		} else {// 有战绩就将list中的ImageView添加到显示战绩的linear
			for (int i = 0; i < zhanjilist.size(); i++) {
				linearLayOutZhanji.setVisibility(View.VISIBLE);
				linearZhanji.setVisibility(View.VISIBLE);
				linearZhanji.addView(zhanjilist.get(i));
			}
		}
	}

	private void getAvatar() {
		new Thread() {

			@Override
			public void run() {
				super.run();

				Bitmap bm = null;
				String name = HemaiDetailInfo.getHemaiDetailInfo().getUserIcon();
				ImageDB.getImageDB().open(BeidanRecordDetailAct.this);

				bm = ImageUtils.getLocalImg(name);

				if (bm == null) {
					bm = ImageUtils.downLoadImg(name);
					if (bm != null) {
						Message msg = handlerHemaiDetail.obtainMessage(REPLACE_IMAGE, bm);
						handlerHemaiDetail.sendMessage(msg);
						ImageUtils.saveImg(name, bm);
					}
				} else {
					Message msg = handlerHemaiDetail.obtainMessage(REPLACE_IMAGE, bm);
					handlerHemaiDetail.sendMessage(msg);
				}
				ImageDB.getImageDB().close();
			}
		}.start();
	}

	// 撤单按钮点击事件
	private void btnChenDanOnClick() {
		final Dialog chenDanDialog = new Dialog(BeidanRecordDetailAct.this, R.style.dialog);
		chenDanDialog.getWindow().setContentView(R.layout.dialog_doublebutton_lotteryrecord_detail);
		TextView tvChenDanMsg = (TextView) chenDanDialog.getWindow().findViewById(R.id.dialog_doublebutton_tv_msg);
		tvChenDanMsg.setText(Language.MSG_REMOVE_SURE);
		Button btnChenDanYes = (Button) chenDanDialog.getWindow().findViewById(R.id.dialog_doublebutton_btn_yes);
		btnChenDanYes.setText(getResources().getString(R.string.ok));
		btnChenDanYes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cheDanThread();
				chenDanDialog.dismiss();
				progress = ViewerUtil.showProgressDialog(BeidanRecordDetailAct.this, Language.MSG_CHEDANING);
			}
		});
		Button btnChenDanNo = (Button) chenDanDialog.getWindow().findViewById(R.id.dialog_doublebutton_btn_no);
		btnChenDanNo.setText(getResources().getString(R.string.cancel));
		btnChenDanNo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				chenDanDialog.dismiss();
			}
		});
		chenDanDialog.show();
	}

	// 撤单通讯线程
	private void cheDanThread() {
		new Thread() {
			@Override
			public void run() {
				if (cheDanCommunicater == null) {
					cheDanCommunicater = new CheDanCommunicater();
				}
				EDebug.o(this, "进入撤单线程");
				Message msg;
				try {
					boolean isSucceed;
					EDebug.o(this, "发起人：" + strfaqiren + "用户：" + UserInfo.getUserInfo().getUserName());
					if (UserInfo.getUserInfo().getUserName().equals(strfaqiren)) {
						EDebug.o(this, "发起人撤单");
						isSucceed = cheDanCommunicater.userOrderCheDan(Config.BEIJINGZUQIUDANCHANG, programId, playId);
					} else {
						EDebug.o(this, "跟单人撤单");
						isSucceed = cheDanCommunicater.isCheDan(Config.BEIJINGZUQIUDANCHANG, uid, playId);
					}
					if (isSucceed) {
						msg = handlerHemaiDetail.obtainMessage(CHEDAN_SUCCEED, cheDanCommunicater.getServerMsg());
					} else {
						msg = handlerHemaiDetail.obtainMessage(CHEDAN_FAIL, cheDanCommunicater.getServerMsg());
					}
					handlerHemaiDetail.sendMessage(msg);
				} catch (Exception e) {
					msg = handlerHemaiDetail.obtainMessage(Config.CONNECTION_FAIL);
					handlerHemaiDetail.sendMessage(msg);
				}
			}
		}.start();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.beidanrecorddetailact_btn_cancel:
			BeidanRecordDetailAct.this.finish();
			break;

		case R.id.beidanrecorddetailact_btn_detail:

			if (isTXT) {
				Bundle bundle = new Bundle();
				bundle.putString("classify", "web");
				bundle.putString(Key.KEY_LOTID, Config.BEIJINGZUQIUDANCHANG);
				bundle.putString("detailurl", Config.TXTURLBEIDAN + playId + "&pid=" + programId);
				startActivity(new Intent(BeidanRecordDetailAct.this, LotteryRecordDetailMoreAct.class).putExtras(bundle));
			}

			break;

		case R.id.beidanrecord_detailact_btn_cheDan:
			btnChenDanOnClick();
			break;

		default:
			break;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		OptionAct.isLoginSucceed = false;
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

}
